package cn.bytepulse.web.admin.service.impl;

import cn.bytepulse.pojo.AjaxResult;
import cn.bytepulse.web.admin.entity.SysRole;
import cn.bytepulse.web.admin.entity.SysUser;
import cn.bytepulse.web.admin.entity.SysUserRole;
import cn.bytepulse.web.admin.mapper.SysRoleMapper;
import cn.bytepulse.web.admin.mapper.SysUserMapper;
import cn.bytepulse.web.admin.mapper.SysUserRoleMapper;
import cn.bytepulse.web.admin.service.SystemUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static cn.bytepulse.utils.PageUtils.startPage;

/**
 * @author jiejiebiezheyang
 * @since 2024-07-10 22:00
 */
@Service
@Transactional
@RequiredArgsConstructor
public class SystemUserServiceImpl implements SystemUserService {

    private final SysUserMapper sysUserMapper;

    private final SysUserRoleMapper sysUserRoleMapper;

    private final SysRoleMapper sysRoleMapper;

    private final BCryptPasswordEncoder bCryptPasswordEncoder;

    /**
     * 角色列表
     */
    @Override
    public AjaxResult list(Integer id) {
        startPage();
        List<SysUser> sysUsers = sysUserMapper.selectListLikeId(id);
        PageInfo<SysUser> pageInfo = PageInfo.of(sysUsers);
        return AjaxResult.success(pageInfo);
    }

    /**
     * 添加角色
     */
    @Override
    public AjaxResult add(SysUser sysUser) {
        String password = bCryptPasswordEncoder.encode("000000");
        SysUser one = new SysUser();
        one.setNickname(sysUser.getNickname());
        one.setUsername(sysUser.getUsername());
        one.setPassword(password);
        one.setLastUpdate(new Date());
        one.setStatus(sysUser.getStatus());
        one.setPhone(sysUser.getPhone());
        sysUserMapper.insert(one);
        return AjaxResult.success();
    }

    /**
     * 更新用户
     */
    @Override
    public AjaxResult update(SysUser sysUser) {
        SysUser one = new SysUser();
        one.setId(sysUser.getId());
        one.setLastUpdate(new Date());
        one.setStatus(sysUser.getStatus());
        one.setPhone(sysUser.getPhone());
        sysUserMapper.updateById(one);
        return AjaxResult.success();
    }

    /**
     * 获取关联
     */
    @Override
    public AjaxResult getRelation(Integer id) {
        QueryWrapper<SysUserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", id);
        List<SysUserRole> userRoles = sysUserRoleMapper.selectList(wrapper);
        List<Integer> roleIds = userRoles.stream().map(SysUserRole::getRoleId).toList();
        return AjaxResult.success(roleIds);
    }

    /**
     * 获取角色简要列表
     */
    @Override
    public AjaxResult roleConcise() {
        List<SysRole> sysRoles = sysRoleMapper.selectConcise();
        return AjaxResult.success(sysRoles);
    }

    /**
     * 更新角色关联
     */
    @Override
    public AjaxResult updateRelation(Integer userId, Integer[] roleIds) {
        QueryWrapper<SysUserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        sysUserRoleMapper.delete(wrapper);
        if (roleIds != null && roleIds.length > 0) {
            List<Integer> integers = Arrays.asList(roleIds);
            sysUserRoleMapper.insertBatchByUserId(userId, integers);
        }
        return AjaxResult.success();
    }

    /**
     * 重置用户密码
     */
    @Override
    public AjaxResult resetPassword(Integer id) {
        String password = bCryptPasswordEncoder.encode("000000");
        SysUser user = new SysUser();
        user.setId(id);
        user.setPassword(password);
        sysUserMapper.updateById(user);
        return AjaxResult.success();
    }
}
